package com.azhuo.core.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.azhuo.core.dao.CustomerDao;
import com.azhuo.core.pojo.Customer;
import com.azhuo.core.service.CustomerService;
import com.azhuo.utils.Page;

@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {

	@Autowired
	private CustomerDao customerDao;

	/**
	 * 查询客户列表
	 */
	@Override
	public Page<Customer> findAllCustomer(Integer page, Integer size, Customer customer) {
		// 设置分页参数
		page = page != null ? page - 1 : 0;
		customer.setStart(page * size);
		customer.setRows(size);

		// 查询总客户列表
		List<Customer> customers = customerDao.selectCustomer(customer);
		// 查询总记录数
		int count = customerDao.selectCustomerCount(customer);
		// result.setTotal(result.getRows().size());

		Page<Customer> result = new Page<Customer>();
		if (customers == null) {
			result.setRows(customers);
		} else {
			result.setRows(customers);
		}
		result.setTotal(count);
		result.setPage(page + 1);
		result.setSize(size);
		
		return result;
	}

	@Override
	public void addCustomer(Customer customer) {

		customerDao.insertCustomer(customer);
	}

	@Override
	public Customer findCustomerByCust_id(Integer cust_id) {
		return customerDao.selectCustomerByCust_id(cust_id);
	}

	@Override
	public void updateCustomer(Customer customer) {
		customerDao.updateCustomer(customer);

	}

	/**
	 * 根据cust_id删除客户信息
	 */
	@Override
	public int deleteCustomer(Integer cust_id) {
		return customerDao.deleteCustomer(cust_id);
	}

}
